package logic;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import database.DataConnection;

/** EarthDefenderConfig class.
 * @author Jesus
 *
 */
public class EarthDefenderConfig {
	
	/** Writes all the parameters into the Config.dat file.
	 * @param userName : The current logged user's nick name.
	 * @param enemyShotProbability : The value of the probability of shooting from an enemy.
	 * @param enemyShotSpeed : The value of the speed of the enemy shot.
	 * @param lives: the initial value of the player life.
	 * @param bouncherEnabled : A boolean that is true if the bounchers are enabled, false otherwise.
	 */
	public static void write(String userName, Integer enemyShotProbability, Integer enemyShotSpeed, Integer lives, Boolean bounchersEnabled) {
		Integer intBounchersEnabled = bounchersEnabled ? 1 : 0;
		try {
			Connection connection = DataConnection.openConection();
			PreparedStatement prepStatement = connection.prepareStatement("Select * from EDCONFIG where nick = ?;");
			prepStatement.setString(1, userName);
			ResultSet resultSet = prepStatement.executeQuery();
			if(resultSet.next()) {
				resultSet.close();
				prepStatement.close();
				//Overwrites the user EDConfig.
				prepStatement = connection.prepareStatement("Update EDCONFIG set eShotProbability = ?, eShotSpeed = ?," +
						" liveNumber = ?, bouncherEnabled = ? where nick = ?;");
				prepStatement.setString(1, enemyShotProbability.toString());
				prepStatement.setString(2, enemyShotSpeed.toString());
				prepStatement.setString(3, lives.toString());
				prepStatement.setString(4, intBounchersEnabled.toString());
				prepStatement.setString(5, userName);
			} else {
				resultSet.close();
				prepStatement.close();
				//Creates new user EDConfig.
				prepStatement = connection.prepareStatement("Insert into EDCONFIG values(?, ?, ?, ?, ?);");
				prepStatement.setString(1, userName);
				prepStatement.setString(2, enemyShotProbability.toString());
				prepStatement.setString(3, enemyShotSpeed.toString());
				prepStatement.setString(4, lives.toString());
				prepStatement.setString(5, intBounchersEnabled.toString());
			}
			prepStatement.executeUpdate();
			prepStatement.close();
			connection.close();
		} catch (SQLException e) {
			
		}
	}
	
	public static ArrayList<Object> load(String userName) throws NoSaveException {
		ArrayList<Object> config = new ArrayList<Object>();
		try{
			Connection connection = DataConnection.openConection();
			PreparedStatement prepStatement = connection.prepareStatement("Select * from EDCONFIG where nick = ?;");
			prepStatement.setString(1, userName);
			ResultSet resultSet = prepStatement.executeQuery();
			if(resultSet.next()) {
				config.add(resultSet.getInt("eShotProbability"));
				config.add(resultSet.getInt("eShotSpeed"));
				config.add(resultSet.getInt("liveNumber"));
				config.add(Utilities.intToBoolean(resultSet.getInt("bouncherEnabled")));
				resultSet.close();
				prepStatement.close();
				connection.close();
			} else {
				resultSet.close();
				prepStatement.close();
				connection.close();
				throw new NoSaveException();
			}
		}catch(SQLException e) {
			
		}
		return config;
	}
}